package com.hboxs.asl.util;

import net.paoding.analysis.analyzer.PaodingAnalyzer;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;

import java.io.IOException;
import java.io.StringReader;

public class PinYinUtils {
    private static HanyuPinyinOutputFormat format = null;

    static {
        format = new HanyuPinyinOutputFormat();
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    }

    /**
     * 得到一个字符串的拼音分词，用空格隔开
     *
     * @param str
     * @return
     */
    public static String getStringPinYin(String str) {

        PaodingAnalyzer analyzer = new PaodingAnalyzer();
        TokenStream ts = analyzer.tokenStream("", new StringReader(str));
        StringBuilder sb = new StringBuilder();
        sb.setLength(0);
        boolean hasnext;
        try {
            hasnext = ts.incrementToken();

            // Token t = ts.next();
            String s1 = "", s2 = "";
            while (hasnext) {
                // AttributeImpl ta = new AttributeImpl();
                CharTermAttribute ta = ts.getAttribute(CharTermAttribute.class);
                // TermAttribute ta = ts.getAttribute(TermAttribute.class);

                s2 = ta.toString() + " ";
                s1 += s2;
                hasnext = ts.incrementToken();
            }
            str = s1;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            analyzer.close();
        }

        String tempPinyin = null;

        for (int i = 0; i < str.length(); ++i) {
            tempPinyin = getCharacterPinYin(str.charAt(i));

            if (tempPinyin == null) {
                // 如果str.charAt(i)非汉字，则保持原样
                sb.append(str.charAt(i));
            } else {
                sb.append(tempPinyin);
            }
        }

        return sb.toString();
    }

    /**
     * 得到一个字符串的中文分词，用逗号隔开
     *
     * @param str
     * @return
     */
    public static String getSplitWord(String str) {

        PaodingAnalyzer analyzer = new PaodingAnalyzer();
        TokenStream ts = analyzer.tokenStream("", new StringReader(str));
        boolean hasnext;
        try {
            hasnext = ts.incrementToken();

            // Token t = ts.next();
            String s1 = "", s2 = "";
            while (hasnext) {
                // AttributeImpl ta = new AttributeImpl();
                CharTermAttribute ta = ts.getAttribute(CharTermAttribute.class);
                // TermAttribute ta = ts.getAttribute(TermAttribute.class);

                s2 = ta.toString() + ",";
                s1 += s2;
                hasnext = ts.incrementToken();
            }
            str = s1;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            analyzer.close();
        }

        return str.toString();
    }

    // 转换单个字符
    public static String getCharacterPinYin(char c) {
        String[] pinyin = null;
        try {
            pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format);
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }

        // 如果c不是汉字，toHanyuPinyinStringArray会返回null
        if (pinyin == null) {
            return null;
        }

        // 只取一个发音，如果是多音字，仅取第一个发音
        return pinyin[0];
    }
}
